<?php

namespace Home\Controller;

use Think\Controller;
use Think\Upload;

class UploadController extends Controller {
	public function upload() {
		$dir = $_REQUEST ['dir'] ? $_REQUEST ['dir'] : 'tupian' . date ( 'Y' );
		if (! strpos ( $dir, '/' )) {
			$fileName = $dir . '-';
			$dir = $dir . '/' . date ( 'Ym' );
		} else {
			$arr = explode ( '/', $dir );
			$fileName = $arr [1] . '-';
		}
		$fileName .= uniqid () . rand ( 10, 99 );
		$savePath = $dir . '/';

		// make_dirs ( $savePath );
		$upload = new Upload ();
		$upload->maxSize = 1024 * 1024 * 2; // 设置附件上传大小
		$upload->exts = array (
				'jpg',
				'gif',
				'png',
				'jpeg'
		);
		// 设置附件上传类型
		$upload->rootPath = C ( 'upload_path' ).'/'; // 根路径
		$upload->savePath = $savePath; // 设置附件上传目录
		$upload->replace = true;
		$upload->saveName = $fileName;
		$upload->autoSub = false;
		$info = $upload->upload ();

		if (! $info) { // 上传错误提示错误信息
			$data ['msg'] = $upload->getError ();
			$data ['status'] = false;
		} else {
			foreach ( $info as $v ) {
				$data ['name'] = $_REQUEST ['pictitle'] ? $_REQUEST ['pictitle'] : $v ['name'];
				$path = '/' . $v ['savepath'] . $v ['savename'];
				$path500 = '/' . $v ['savepath'] . str_replace ( '.', '_500_500.', $v ['savename'] );
				$path150 = '/' . $v ['savepath'] . str_replace ( '.', '_150_150.', $v ['savename'] );
			}

			// 缩略图
			$image = new \Think\Image ();
			$image->open ( C ( 'upload_path' ) . $path );
			$size = $image->size ();
			if ($size [0] > 500 || $size [1] > 500) {
				$image->thumb ( 500, 500, \Think\Image::IMAGE_THUMB_CENTER )->text ( C ( 'watermark' ), C ( 'watermark_font' ), 12, C ( 'watermark_colour' ), \Think\Image::IMAGE_WATER_SOUTHEAST )->save ( C ( 'upload_path' ) . $path500, null, false );
			} else {
				$image->text ( C ( 'watermark' ), C ( 'watermark_font' ), 12, C ( 'watermark_colour' ), \Think\Image::IMAGE_WATER_SOUTHEAST )->save ( C ( 'upload_path' ) . $path500, null, false );
			}
			$image->open ( C ( 'upload_path' ) . $path );
			$image->thumb ( 150, 150, \Think\Image::IMAGE_THUMB_CENTER )->save ( C ( 'upload_path' ) . $path150, null, true );
			$data ['status'] = 1;
			$data ['thumb150'] = $path150;
			$data ['thumb500'] = $path500;
			$data ['path'] = $path;
		}

		// 把上传记录保存到cookie
		$str = cookie ( 'uploadBlogHistory' );
		$str = C ( 'img_server' ) . $path500 . 'ue_separate_ue' . $str;
		cookie ( 'uploadBlogHistory', $str, array (
				'expire' => 86400 * 360
		) );

		if ($_REQUEST ['ueditor']) {
			echo "{'url':'" . C ( 'img_server' ) . $path500 . "','title':'" . $data ['name'] . "','original':'" . $data ["path"] . "','state':'SUCCESS'}";
		} elseif (IS_AJAX) {
			$this->ajaxReturn ( $data );
		} else {
			if ($data ['status']) {
				echo '<script>parent.uploadCallBack("' . C ( 'img_server' ) . '","' . $path . '");</script>';
			} else {
				echo '<script>parent.uploadCallBack("' . $data ['msg'] . '");</script>';
			}
		}
	}
 
	/**
	 * 获取用户上传的历史图片
	 */
	public function getUpload() {
		$do = $_GET ['do'];
		$cutline = 'ue_separate_ue';
		echo cookie ( 'uploadBlogHistory' );
	}
}